package cn.itcast.p2p20.utils;

import android.app.ActionBar;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

import cn.itcast.p2p20.bean.UserBean;

/**
 * Created by Administrator on 2016/7/23.
 */
public class Util {

    private static SharedPreferences sharedPreferences;
    private static Toast toast;

    //已经使用了导航
    public static void hasStartGuideToMainUI(Context context) {

        if (sharedPreferences == null) {

            sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }

        sharedPreferences.edit().putBoolean("HASSTARTGUIDETOMAINUI", true).commit();

    }

    //判断是否已经导航
    public static boolean isStartGuideToMainUI(Context context) {

        if (sharedPreferences == null) {

            sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }

        return sharedPreferences.getBoolean("HASSTARTGUIDETOMAINUI", false);
    }

    public static float dipToPx(Context context, int dip) {

        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.getResources().getDisplayMetrics());
    }

    public static View createTagView(Context mContext, String tag) {

        TextView textView = new TextView(mContext);
        textView.setGravity(Gravity.CENTER);
        textView.setText(tag);
        textView.setLayoutParams(new ViewGroup.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT));
        int padding = (int) Util.dipToPx(mContext, 5);
        textView.setPadding(padding, padding, padding, padding);

        ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT);
        int margin = (int) Util.dipToPx(mContext, 5);
        params.setMargins(margin, margin, margin, margin);
        textView.setLayoutParams(params);

        int randomTextSize = mRandom.nextInt(18) + 10;
        textView.setTextSize(randomTextSize);

        //选择器 StateListDrawable xml Selector
        StateListDrawable stateListDrawable = new StateListDrawable();

        //按下的背景,灰色
        GradientDrawable pressDrawable = new GradientDrawable();
        pressDrawable.setCornerRadius(Util.dipToPx(mContext, 4));
        pressDrawable.setStroke((int) Util.dipToPx(mContext, 1), Color.BLUE);
        pressDrawable.setColor(Color.GRAY);

        //添加到选择器,按下
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressDrawable);


        //需要通过代码动态的生成不同颜色shape,GradientDrawable
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadius(Util.dipToPx(mContext, 4));
        gradientDrawable.setStroke((int) Util.dipToPx(mContext, 1), Color.BLUE);

        int randomColor = generaRandomColor(mContext);
        gradientDrawable.setColor(randomColor);

        //添加到选择器,默认
        stateListDrawable.addState(new int[]{}, gradientDrawable);


        textView.setTextColor(generaTextColor(randomColor));
        textView.setBackgroundDrawable(stateListDrawable);
        return textView;
    }


    //判断当前背景演示深浅，根据背景颜色的深浅设计字体颜色
    private static int generaTextColor(int randomColor) {

        //0~ 255
        int color = (Color.red(randomColor) + Color.green(randomColor) + Color.blue(randomColor)) / 3;
        if (color > 0xff / 2) {

            return Color.BLACK;
        } else {

            return Color.WHITE;
        }
    }

    private static Random mRandom = new Random();

    public static int generaRandomColor(Context mContext) {

        //0 ~ 255 0xff
        int red = mRandom.nextInt(0xff);
        int green = mRandom.nextInt(0xff);
        int blue = mRandom.nextInt(0xff);
        return Color.argb(0xff, red, green, blue);
    }

    public static void showToast(Context mContext, String text) {

        if (toast == null) {
            toast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
        }
        toast.setText(text);
        toast.show();
    }

    public static UserBean getUserInfo(Context context) {

        if (sharedPreferences == null) {

            sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }

        String username = sharedPreferences.getString("username", "");
        String token = sharedPreferences.getString("token", "");
        String headUrl = sharedPreferences.getString("headUrl", "");

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(token) || TextUtils.isEmpty(headUrl)) {

            return null;
        }

        return new UserBean(username, token, headUrl);
    }


    public static void saveUserInfo(Context context, UserBean user) {

        if (sharedPreferences == null) {

            sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }

        sharedPreferences.edit().putString("username", user.getUsername()).commit();
        sharedPreferences.edit().putString("token", user.getToken()).commit();
        sharedPreferences.edit().putString("headUrl", user.getHeadUrl()).commit();
    }


}

